package com.cgly.medical.service.impl;

import com.cgly.medical.domain.*;
import com.cgly.medical.mapper.DoctorMapper;
import com.cgly.medical.mapper.DrugMapper;
import com.cgly.medical.mapper.DrugOrderMapper;
import com.cgly.medical.mapper.PatientMapper;
import com.cgly.medical.model.DrugOrderModel;
import com.cgly.medical.model.EndOrder;
import com.cgly.medical.service.DrugOrderService;
import com.cgly.medical.util.Msg;
import com.cgly.medical.util.SnowFlakeGenerateIdWorkerutil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DrugOrderServiceImpl implements DrugOrderService {

    @Resource
    DrugOrderMapper drugOrderMapper;

    @Resource
    DoctorMapper doctorMapper;

    @Resource
    DrugMapper drugMapper;

    @Resource
    PatientMapper patientMapper;


    //查询所有的订单信息
    @Override
    public Msg getAllDrugOrder(Integer page,Integer limit) {
        List<DrugOrder> allDrugOrder1 = drugOrderMapper.getAllDrugOrder();
        PageHelper.startPage(page,limit);
        List<DrugOrder> allDrugOrder = drugOrderMapper.getAllDrugOrder();
        List<DrugOrderModel> drugOrderModelList=new ArrayList<>();
        if(allDrugOrder.size()==0){
            return Msg.fail().mess("药品订单查询失败");
        }else{
            drugOrderModelList=toModel(allDrugOrder);
            PageInfo<DrugOrderModel> drugOrderModels=new PageInfo<>(drugOrderModelList,5);
            drugOrderModels.setTotal(allDrugOrder1.size());
            return Msg.success().data("drugOrderModels",drugOrderModels);
        }
    }

    @Override
    public Msg getSomeDrugOrder(DrugOrderModel drugOrderModel) {
        List<DrugOrderModel> drugOrderModelArrayList=new ArrayList<>();
        if(drugOrderModel.getPatientName()!=null){
            Patient temp=new Patient();
            temp.setPname(drugOrderModel.getPatientName());
            List<Patient> somePatientTemp = patientMapper.getSomePatient(temp);
            PageHelper.startPage(drugOrderModel.getPn(),drugOrderModel.getSize());
            List<Patient> somePatient = patientMapper.getSomePatient(temp);
            for(Patient patient:somePatient){
               drugOrderModel.setPatientId(patient.getId());
                List<DrugOrder> someDrugOrder = drugOrderMapper.getSomeDrugOrder(drugOrderModel);
                List<DrugOrderModel> drugOrderModels = toModel(someDrugOrder);
                drugOrderModelArrayList.addAll(drugOrderModels);
            }
            PageInfo<DrugOrderModel> drugOrderModelList=new PageInfo<>(drugOrderModelArrayList,5);
            drugOrderModelList.setTotal(somePatientTemp.size());
            return Msg.success().data("drugOrderModelList",drugOrderModelList);
        }else{
            List<DrugOrder> someDrugOrderTemp = drugOrderMapper.getSomeDrugOrder(drugOrderModel);
            PageHelper.startPage(drugOrderModel.getPn(),drugOrderModel.getSize());
            List<DrugOrder> someDrugOrder = drugOrderMapper.getSomeDrugOrder(drugOrderModel);
            List<DrugOrderModel> drugOrderModels = toModel(someDrugOrder);
            drugOrderModelArrayList.addAll(drugOrderModels);
            PageInfo<DrugOrderModel> drugOrderModelList=new PageInfo<>(drugOrderModelArrayList,5);
            drugOrderModelList.setTotal(someDrugOrderTemp.size());
            return Msg.success().data("drugOrderModelList",drugOrderModelList);
        }
    }

    @Override
    public Msg deleteDrugOrder(DrugOrder drugOrder) {
        int row = drugOrderMapper.deleteDrugOrder(drugOrder);
        if(row==0){
            return Msg.fail().mess("订单删除失败");
        }else{
            return Msg.success().data("row",row);
        }
    }

    @Override
    public Msg getOneDrugOrder(DrugOrder drugOrder) {
        DrugOrder oneDrugOrder = drugOrderMapper.getOneDrugOrder(drugOrder);
        List<DrugOrder> list=new ArrayList<>();
        if(oneDrugOrder==null){
            return Msg.fail().mess("订单查询有误");
        }else{
            list.add(oneDrugOrder);
            List<DrugOrderModel> drugOrderModels = toModel(list);
            return Msg.success().data("drugOrderModel",drugOrderModels.get(0));
        }
    }

    @Override
    public Msg updateDrugOrder(DrugOrderModel drugOrderModel) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        drugOrderModel.setUpdateTime(format);
        int row = drugOrderMapper.updateDrugOrder(toDrugOrder(drugOrderModel));
        if(row==0){
            return Msg.fail().mess("订单更新失败");
        }else{
            return Msg.success().data("row",row);
        }
    }

    @Override
    public Msg insertDrugOrder(DrugOrderModel drugOrderModel) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        drugOrderModel.setCreateTime(format);
        drugOrderModel.setUpdateTime(format);
        int row = drugOrderMapper.insertDrugOrder(toDrugOrder(drugOrderModel));
        if(row==0){
            return Msg.fail().mess("订单更新失败");
        }else{
            List<DrugOrder> drugOrders = drugOrderMapper.selectDrugOrderMaxID();
            DrugOrder drugOrder = drugOrders.get(0);
            return Msg.success().data("row",row).data("drugOrderRow",drugOrder);
        }
    }

    @Override
    public DrugOrder getDrugOrderById(int orderId) {
        DrugOrder order=drugOrderMapper.getDrugOrderById(orderId);
        return order;
    }

    @Override
    public Msg updatePatientHistoryOrder(List<EndOrder> params) {

        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        SnowFlakeGenerateIdWorkerutil snowFlakeGenerateIdWorkerutil = new SnowFlakeGenerateIdWorkerutil(0L,0L);
        String id = snowFlakeGenerateIdWorkerutil.generateNextId();
        System.out.println(id);
        int num=0;
            for(EndOrder endOrder:params ) {
                DrugOrderModel drugOrderModel = new DrugOrderModel();
                drugOrderModel.setEndOrderId(id);
                drugOrderModel.setUpdateTime(format);
                drugOrderModel.setOrderId(endOrder.getOrderId());
                drugOrderModel.setPatientId(endOrder.getPatientId());
                drugOrderModel.setDrugId(endOrder.getDrugId());
                num = drugOrderMapper.updateDrugOrder(toDrugOrder(drugOrderModel));
            }
        if(num==0){
            return Msg.fail().mess("订单更新失败");
        }else{
            return Msg.success().data("row",id);
        }
    }

    @Override
    public List<DrugOrder> getDrugOrderByEndOrderId(String endOrderId) {
        List<DrugOrder> drugOrders=drugOrderMapper.getDrugOrderByEndOrderId(endOrderId);
        return drugOrders;
    }

    //封装方法：DrugOrder转为DrugOrderModel
    public List<DrugOrderModel> toModel(List<DrugOrder> allDrugOrder){
        List<DrugOrderModel> drugOrderModels=new ArrayList<>();
        for(DrugOrder drugOrder:allDrugOrder){
            DrugOrderModel temp=new DrugOrderModel();
            temp.setOrderId(drugOrder.getOrderId());
            temp.setPatientId(drugOrder.getPatientId());
            temp.setDrugId(drugOrder.getDrugId());
            temp.setDoctorId(drugOrder.getDoctorId());
            temp.setPrice(drugOrder.getPrice());
            temp.setNumber(drugOrder.getNumber());
            temp.setDiscount(drugOrder.getDiscount());
            temp.setTotalPrice(drugOrder.getTotalPrice());
            temp.setCreateTime(drugOrder.getCreateTime());
            temp.setUpdateTime(drugOrder.getUpdateTime());
            temp.setStatus(drugOrder.getStatus());
//                System.out.println("===="+drugOrder);
            Doctor doctor = doctorMapper.selectDoctorByid(drugOrder.getDoctorId().toString());
            temp.setDoctorName(doctor.getName());
            Drug drug = drugMapper.getDrugById(drugOrder.getDrugId());
            temp.setDrugName(drug.getDrugName());
            Patient patient = patientMapper.getPatientById(drugOrder.getPatientId());
            temp.setPatientName(patient.getPname());
            drugOrderModels.add(temp);
        }
        return drugOrderModels;
    }

    //封装方法  DrugOrderModel转为DrugOrder
    public DrugOrder toDrugOrder(DrugOrderModel drugOrderModel){
        DrugOrder drugOrder=new DrugOrder();
        drugOrder.setOrderId(drugOrderModel.getOrderId());
        drugOrder.setDrugId(drugOrderModel.getDrugId());
        drugOrder.setPatientId(drugOrderModel.getPatientId());
        drugOrder.setEndOrderId(drugOrderModel.getEndOrderId());
        drugOrder.setDoctorId(drugOrderModel.getDoctorId());
        drugOrder.setPrice(drugOrderModel.getPrice());
        drugOrder.setTotalPrice(drugOrderModel.getTotalPrice());
        drugOrder.setStatus(drugOrderModel.getStatus());
        drugOrder.setCreateTime(drugOrderModel.getCreateTime());
        drugOrder.setUpdateTime(drugOrderModel.getUpdateTime());
        drugOrder.setDiscount(drugOrderModel.getDiscount());
        drugOrder.setNumber(drugOrderModel.getNumber());
        return drugOrder;
    }
}
